package demo03;

import demo02.Food;

import java.util.Arrays;
import java.util.concurrent.atomic.AtomicInteger;

public class FilmManager {
    //定义一个用来存储的仓库
    private Film[] arr = {};
    //设置数组的默认初始长度
    private  int  default_capability=5;
    //线程安全的
    private AtomicInteger ai=new AtomicInteger();

    public FilmManager(){
        ai.set(1001);
        Film f1 = new Film("女儿国", "isOnline", 7.5, 38.8 );
        Film f2 = new Film("西游记", "noOnline", 6.9, 25.4);
        Film f3 = new Film("星球大战", "isOnline", 8.8, 39.9);
        Film f4 = new Film("变形金刚", "noOnline", 7.3, 58.8);
        Film f5 = new Film("鬼吹灯", "isOnline", 9.5, 45.6);
        this.addFilm(f1);
        this.addFilm(f2);
        this.addFilm(f3);
        this.addFilm(f4);
        this.addFilm(f5);
    }

    //用来储存实际保存的学生人数
    private  int size;

    public boolean addFilm(Film film){
        //判断数组是否到达容量的上线
        if (size +1>arr.length){
            //一旦size+1的值超过当前数组能够容量，则数组需要扩容
            int min=Math.min(size,default_capability);
            int capability;
            if (min<default_capability){
                capability=default_capability;
            }else{
                capability=size+(size >> 1);
            }
            Film[] newArr=new Film[capability];
            System.arraycopy(this.arr,0,newArr,0,size);
            this.arr=newArr;
        }
        //设置学号自增长

        film.setfId(ai.getAndIncrement());
        arr[size++] = film;
        return true;
    }

    public boolean updateFilm(String name){
        for (int i=0;i<size;i++){
            if (arr[i].getfName().equals(name)){
                if (arr[i].getIsOnline().equals("isOnline")){
                    arr[i].setIsOnline("noOnline");
                }else if (arr[i].getIsOnline().equals("noOnline")){
                    arr[i].setIsOnline("isOnline");
                }
                return true;
            }
        }
        return false;
    }

    public Film[] selectIsOnline(int state){
        Film[] film = Arrays.copyOf(arr, size);
        int count=0;
        if (state==2){
            for (int i = 0; i <film.length ;i++) {
                if (film[i].getIsOnline().equals("null")){
                    film[i].setIsOnline("isOnline");
                }
                if (film[i].getIsOnline().equals("noOnline")||film[i].getIsOnline().equals("null")) {
                    film[i].setIsOnline("null");
                    count++;
                    continue;
                }
                for (int j = 0; j < film.length - 1 - i; j++) {
                    if (film[j+1].getIsOnline().equals("noOnline")||film[j+1].getIsOnline().equals("null"))continue;
                    Film f1 = film[j];
                    Film f2 = film[j + 1];
                    if (f1.getPrice() > f2.getPrice()) {
                        Film temp = f1;
                        film[j] = f2;
                        film[j + 1] = temp;
                    }
                }
            }
        } else  if (state==1){
            for (int i = 0; i <film.length;i++){
                if (film[i].getIsOnline().equals("null")){
                    film[i].setIsOnline("isOnline");
                }
                if (film[i].getIsOnline().equals("noOnline")||film[i].getIsOnline().equals("null")) {
                    film[i].setIsOnline("null");
                    count++;
                    continue;
                }
                for (int j = 0; j < film.length - 1 - i; j++) {
                    if (film[j+1].getIsOnline().equals("noOnline")||film[j+1].getIsOnline().equals("null"))continue;
                    Film f1 = film[j];
                    Film f2 = film[j + 1];
                    if (f1.getScore()>f2.getScore()){
                        Film temp = f1;
                        film[j] = f2;
                        film[j + 1] = temp;
                    }
                }
            }
        }
        Film[] newfilm=new Film[size-count];
        int index=0;
        for (int i=0;i<size;i++){
            if (film[i].getIsOnline().equals("null"))continue;
            newfilm[index++]=film[i];
        }
        return newfilm;
    }

    public Film[] selectNoOnline(int state){
        Film[] film1 = Arrays.copyOf(arr, size);
        int count=0;
        if (state==2){
            for (int i = 0; i <film1.length ;i++) {
                if (film1[i].getIsOnline().equals("null")){
                    film1[i].setIsOnline("noOnline");
                }
                if (film1[i].getIsOnline().equals("isOnline")||film1[i].getIsOnline().equals("null")) {
                    film1[i].setIsOnline("null");
                    count++;
                    continue;
                }
                for (int j = 0; j < film1.length - 1 - i; j++) {
                    if (film1[j+1].getIsOnline().equals("isOnline")||film1[j+1].getIsOnline().equals("null"))continue;
                    Film f1 = film1[j];
                    Film f2 = film1[j + 1];
                    if (f1.getPrice() > f2.getPrice()) {
                        Film temp = f1;
                        film1[j] = f2;
                        film1[j + 1] = temp;
                    }
                }
            }
        }else if (state==1){
            for (int i = 0; i <film1.length ;i++){
                if (film1[i].getIsOnline().equals("null")){
                    film1[i].setIsOnline("noOnline");
                }
                if (film1[i].getIsOnline().equals("isOnline")||film1[i].getIsOnline().equals("null")) {
                    film1[i].setIsOnline("null");
                    count++;
                    continue;
                }
                for (int j = 0; j < film1.length - 1 - i; j++) {
                    if (film1[j+1].getIsOnline().equals("isOnline")||film1[j+1].getIsOnline().equals("null"))continue;
                    Film f1 = film1[j];
                    Film f2 = film1[j + 1];
                    if (f1.getScore()>f2.getScore()){
                        Film temp = f1;
                        film1[j] = f2;
                        film1[j + 1] = temp;
                    }
                }
            }
        }
        Film[] newfilm1=new Film[size-count];
        int index=0;
        for (int i=0;i<size;i++){
            if (film1[i].getIsOnline().equals("null"))continue;
            newfilm1[index++]=film1[i];
        }
        return newfilm1;
    }
}
